package com.rightsidetech.http.controller.admin.user;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.ModifyLog;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.UpdateBlack;
import com.rightsidetech.user.dtos.requst.UserInfoReq;
import com.rightsidetech.user.dtos.response.UserInfoDetailsResp;
import com.rightsidetech.user.dtos.response.UserInfoResp;
import com.rightsidetech.user.service.UserInfoService;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

/**
 * @Package: com.rightsidetech.http.controller.admin.user
 * @ClassName: UserController
 * @Author: yinj
 * @CreateTime: 2021/10/19 14:05
 * @Description: 用户信息相关
 */
@RestController
@RequestMapping("/admin/user")
@ShenyuSpringMvcClient(path = "/admin/user/**")
@Api(tags = "用户相关api")
public class UserInfoController extends BaseController {

    @Resource
    private UserInfoService userInfoService;


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/list")
    @ApiOperation("用户列表")
    public ResponseData<PageInfo<UserInfoResp>> listUser(UserInfoReq req){
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return userInfoService.listuser(req);

    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/detals")
    @ApiOperation("详情")
    public ResponseData<UserInfoDetailsResp> findDetals(Long id){
        return userInfoService.findDetals(id);

    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/add_blacklist")
    @ApiOperation("加入/移出黑名单")
    @ModifyLog(serviceClass = UserInfoService.class)
    public ResponseData addBlacklist(@RequestBody UpdateBlack req){
        Long userInfoId = req.getId();
        Byte status = req.getStatus();
        if (StringUtils.isEmpty(userInfoId) || StringUtils.isEmpty(status)) {
            return ResponseData.fail(CodeIdEnum.IllegalParameter);
        }
        if (status != 0 && status != -1){
            return ResponseData.fail("状态参数不合法");
        }
        ResponseData responseData = userInfoService.addBlacklist(userInfoId, status);
        if (responseData.getCodeId() == 1 && status == -1){
            //退出登录
            userInfoService.logout(userInfoId);
        }
        return responseData;
    }




}
